package com.sumfun.blog.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sumfun.blog.annotation.RequireLogin;
import com.sumfun.blog.common.BaseResponse;
import com.sumfun.blog.common.ErrorCode;
import com.sumfun.blog.common.PageRequest;
import com.sumfun.blog.common.ResultUtils;
import com.sumfun.blog.exception.BusinessException;
import com.sumfun.blog.model.domain.TravelCheckins;
import com.sumfun.blog.service.TravelCheckinsService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 旅行打卡Controller
 *
 * @author sumfun
 * @date 2024-04-21
 */
@Api(value = "旅行打卡信息Controller", tags = {"旅行打卡信息"})
@RestController
@RequestMapping("/travel")
public class TravelCheckinsController {
    
    @Resource
    private TravelCheckinsService travelCheckinsService;

    @RequireLogin
    @PostMapping("/add")
    public BaseResponse<Boolean> addTravel(@RequestBody TravelCheckins travelCheckins, HttpServletRequest request){
        //判空
        if(travelCheckins == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean save = travelCheckinsService.save(travelCheckins);
        return ResultUtils.success(save);
    }

    @RequireLogin
    @DeleteMapping("/{id}")
    public BaseResponse<Boolean> deleteTravel(@PathVariable Long id) {
        if(id < 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean res = travelCheckinsService.removeById(id);
        return ResultUtils.success(res);
    }

    @RequireLogin
    @PostMapping("/update")
    public BaseResponse<Boolean> updateTravel(@RequestBody TravelCheckins travelCheckins, HttpServletRequest request) {
        if(travelCheckins == null || travelCheckins.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean res = travelCheckinsService.updateById(travelCheckins);
        return ResultUtils.success(res);
    }

    @GetMapping("/get")
    public BaseResponse<TravelCheckins> getTravelById(long id) {
        if(id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        TravelCheckins travel = travelCheckinsService.getById(id);
        return ResultUtils.success(travel);
    }

    @GetMapping("/list")
    public BaseResponse<List<TravelCheckins>> getTravelList() {
        List<TravelCheckins> travelList;
        travelList = travelCheckinsService.list();
        return ResultUtils.success(travelList);
    }
    //
    @GetMapping("/list/page")
    public BaseResponse<Page> getTravelListByPage(PageRequest travelQueryRequest) {
        Page<TravelCheckins> page = new Page(travelQueryRequest.getPageNum(), travelQueryRequest.getPageSize());
        Page<TravelCheckins> page1 = travelCheckinsService.page(page);
        return ResultUtils.success(page1);
    }

}
